package logic

import (
	"context"
	"gorm.io/gorm"
	"hotel/models/mysql"

	"hotel/userrpc/internal/svc"
	"hotel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *userrpc.UpdateRequest) (*userrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:    gorm.Model{ID: uint(in.Id)},
		Sex:      in.Sex,
		Nickname: in.Nickname,
		Start:    in.Start,
	}
	err := mysql.Db.Debug().Model(&user).Updates(&user).Error
	if err != nil {
		return nil, err
	}

	return &userrpc.UpdateResponse{Id: int64(user.ID)}, nil
}
